﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using System.Web.Profile;
using System.Web.Security;
using MyBet.Web.Resources;
using ParieurPro.DAL.DatabaseModel;
using ParieurPro.DAL;
using System.Net.Mail;
   
namespace MyBet.Web.Services
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class PredictionService
    {

        [OperationContract]
        public List<BetObject> GetPrediction(Guid userId,
                                             int eventId)
        {
            using (MyBetDBEntities dbContext = new MyBetDBEntities())
            {    
                List<BetObject> retList = new List<BetObject>();
                IList<UserBets> betList = BetObject.GetBetsInDatabase(dbContext, userId, eventId);
                foreach (UserBets b in betList)
                {
                    retList.Add(BetObject.CopyFromDatabaseObject(b));
                }
                return retList;
            }
        }

        [OperationContract]
        public BetObject.PredictionValidationStatus SetPrediction(Guid userId,
                                          int eventId,
                                          List<int> matchId,
                                          List<int> scoreHome,
                                          List<int> scoreAway,
                                          string eventName,
                                          bool sendMail,
                                          out ParieurProFault myFault)
        {
            using (MyBetDBEntities dbContext = new MyBetDBEntities())
            {
                BetObject.PredictionValidationStatus status=BetObject.PredictionValidationStatus.GeneralFailure;
                myFault = null;
                try
                {
                    // Validation des paris
                    status = BetObject.SetPrediction(dbContext, userId, eventId, matchId, scoreHome, scoreAway, DateTime.UtcNow);
                    if (sendMail)
                    {
                        // Envoi d'un mail
                        MembershipUser currentUser = Membership.GetUser(userId);
                        if (currentUser != null)
                        {
                            List<MatchInfo> matchInfo = new List<MatchInfo>();
                            matchInfo = MatchObject.GetMatchsInfos(dbContext, matchId);
                            BetObject.SendBetSummary(currentUser.Email, eventName, matchInfo, scoreHome, scoreAway);
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Trace.WriteLine(String.Format("BetObject.PredictionValidationStatus.SetPrediction {0}",ex));
                    myFault = new ParieurProFault() { FaultType = "Exception", Message = ex.ToString() };
                }
                return status;
            }
        }

    }

}
